package net.stogi.unicore.beyond.ui.client.taskeditor.propertyeditor;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import net.stogi.common.gwt.dispatch.client.core.ActionManager;
import net.stogi.common.gwt.frame.client.core.EventBus;
import net.stogi.common.gwt.frame.client.mvp.AbstractPresenter;
import net.stogi.common.mvp.core.behavior.BehaviorWithValue;
import net.stogi.unicore.beyond.api.model.InputDefinition;
import net.stogi.unicore.beyond.api.model.PropertyValue;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskoutline.InputDefinitionSelectedEvent;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskoutline.InputDefinitionSelectedHandler;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskoutline.InputGroupSelectedEvent;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskoutline.InputGroupSelectedHandler;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskoutline.OutputDefinitionSelectedEvent;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskoutline.OutputDefinitionSelectedHandler;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskoutline.OutputGroupSelectedEvent;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskoutline.OutputGroupSelectedHandler;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskpreview.TaskPreviewClosedEvent;
import net.stogi.unicore.beyond.ui.client.taskeditor.taskpreview.TaskPreviewClosedHandler;

import com.google.gwt.core.client.GWT;

class PropertyViewerPresenter extends AbstractPresenter {

	private PropertyViewerView propertyViewerView;

	private InputDefinition inputDefinition;

	public PropertyViewerPresenter(PropertyViewerView propertyViewerView, ActionManager actionManager, EventBus eventBus) {
		super(actionManager, eventBus);
		this.propertyViewerView = propertyViewerView;
		initBehavior();
		initHandlers();
	}

	@Override
	protected void initBehavior() {
		propertyViewerView.setPropertyDoubleClicked(new BehaviorWithValue<String>() {

			@Override
			public void behave(String value) {
				GWT.log("PropertyViewerPresenter.onPropertyDoubleClicked: selected property [ value = '" + value + "' ]");
				if (value != null) {
					PropertyValue property = inputDefinition.getProperty(value);
					eventBus.fireEvent(new OpenPropertyEditorEvent(property));
				}
			}
		});
	}

	@Override
	protected void initHandlers() {
		eventBus.addHandler(InputDefinitionSelectedEvent.TYPE, new InputDefinitionSelectedHandler() {

			@Override
			public void onInputDefinitionSelected(InputDefinitionSelectedEvent event) {
				inputDefinition = event.getInputDefinition();
				internalDisplayInputDefinitionProperties(inputDefinition);
			}
		});
		eventBus.addHandler(InputGroupSelectedEvent.TYPE, new InputGroupSelectedHandler() {

			@Override
			public void onInputGroupSelected(InputGroupSelectedEvent event) {
				propertyViewerView.clear();
			}
		});
		eventBus.addHandler(OutputDefinitionSelectedEvent.TYPE, new OutputDefinitionSelectedHandler() {

			@Override
			public void onOutputDefinitionSelected(OutputDefinitionSelectedEvent event) {
				// TODO: Property editor for output definition ?
				// outputDefinition = event.getOutputDefinition();
				// internalDisplayInputDefinitionProperties(outputDefinition);
				propertyViewerView.clear();
			}
		});
		eventBus.addHandler(OutputGroupSelectedEvent.TYPE, new OutputGroupSelectedHandler() {

			@Override
			public void onOutputGroupSelected(OutputGroupSelectedEvent event) {
				propertyViewerView.clear();
			}
		});
		eventBus.addHandler(PropertyChangedEvent.TYPE, new PropertyChangedHandler() {

			@Override
			public void onPropertyChanged(PropertyChangedEvent event) {
				internalDisplayInputDefinitionProperties(inputDefinition);
			}
		});
		eventBus.addHandler(TaskPreviewClosedEvent.TYPE, new TaskPreviewClosedHandler() {

			@Override
			public void onTaskPreviewClosed(TaskPreviewClosedEvent event) {
				propertyViewerView.clear();
			}
		});
	}

	private void internalDisplayInputDefinitionProperties(InputDefinition inputDefinition) {
		Map<String, String> properties = new HashMap<String, String>();
		for (Entry<String, PropertyValue> entry : inputDefinition.getProperties().entrySet()) {
			properties.put(entry.getKey(), entry.getValue().getValue());
		}
		propertyViewerView.displayProperties(properties);
	}
}
